package com.chf.control;

import com.chf.Bean.UserInfoBean;
import com.chf.model.MyModel;
import com.chf.utils.MyUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
//添加用户
@WebServlet(name = "UserInfoAddServlet", value = "/UserInfoAddServlet")
public class UserInfoAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserInfoBean userInfoBean = MyUtils.GetClass(UserInfoBean.class);
        //如果id不为null，就判定为修改用户信息
        if(req.getParameter("userId")!=null && !req.getParameter("userId").equals("")){
            System.out.println("userID-------"+req.getParameter("userId"));
            userInfoBean.setUserId(Integer.valueOf(req.getParameter("userId")));
        }
        //若id为null，就是添加用户信息
        userInfoBean.setUserName(req.getParameter("userName"));
        userInfoBean.setUserPwd(req.getParameter("userPwd"));
        userInfoBean.setUserTel(req.getParameter("userTel"));
        userInfoBean.setUserBirthday(MyUtils.StrConverDate(req.getParameter("userRegisteDate"),"yyyy-MM-dd"));

        int i = MyModel.AddUserInfo(userInfoBean);
        if (i>0){
            //添加/修改完成后查询所有用户信息，将其保存到session中
            List<UserInfoBean> list = MyModel.SeleteAllUserInfo();
            req.getSession().setAttribute("UserList",list);

            req.getRequestDispatcher("WEB-INF/viewpages/userInfoB/SelectAllUserInfo.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("WEB-INF/viewpages/loginedmain.jsp").forward(req,resp);
        }
    }
}
